package com.peanut.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.peanut.model.CompanyTreeNode;
import com.peanut.persistence.DataQualityDaoIbatis;

@Service
public class DataQualityService {
	private DataQualityDaoIbatis dataQualityDao;
	
	@Autowired
	public void setDataQualityDao(DataQualityDaoIbatis dataQualityDao) {
		this.dataQualityDao = dataQualityDao;
	}
	
	public List queryNewestProblemInCompany(Map param, int offset, int limit) {
		return dataQualityDao.queryNewestProblemInCompany(param, offset, limit);
	}
	
	public long queryNewestProblemInCompanyCount(Map param) {
		return dataQualityDao.queryNewestProblemInCompanyCount(param);
	}
	
	public List queryNewestProblemInProduct(Map param, int offset, int limit) {
		return dataQualityDao.queryNewestProblemInProduct(param, offset, limit);
	}
	
	public long queryNewestProblemInProductCount(Map param) {
		return dataQualityDao.queryNewestProblemInProductCount(param);
	}
	
	public List queryNewestProblemInCheckPoint(Map param, int offset, int limit) {
		return dataQualityDao.queryNewestProblemInCheckPoint(param, offset, limit);
	}
	
	public long queryNewestProblemInCheckPointCount(Map param) {
		return dataQualityDao.queryNewestProblemInCheckPointCount(param);
	}
	
	public List queryConventionDataInCompany(Map param, int offset, int limit) {
		return dataQualityDao.queryConventionDataInCompany(param, offset, limit);
	}
	
	public long queryConventionDataInCompanyCount(Map param) {
		return dataQualityDao.queryConventionDataInCompanyCount(param);
	}
	
	public List queryConventionDataInProduct(Map param, int offset, int limit) {
		return dataQualityDao.queryConventionDataInProduct(param, offset, limit);
	}
	
	public long queryConventionDataInProductCount(Map param) {
		return dataQualityDao.queryConventionDataInProductCount(param);
	}
	
	public List queryConventionDataInCheckPoint(Map param, int offset, int limit) {
		return dataQualityDao.queryConventionDataInCheckPoint(param, offset, limit);
	}
	
	public long queryConventionDataInCheckPointCount(Map param) {
		return dataQualityDao.queryConventionDataInCheckPointCount(param);
	}
	
	public List queryConsistencyDataInCompany(Map param, int offset, int limit) {
		return dataQualityDao.queryConsistencyDataInCompany(param, offset, limit);
	}
	
	public long queryConsistencyDataInCompanyCount(Map param) {
		return dataQualityDao.queryConsistencyDataInCompanyCount(param);
	}
	
	public List queryConsistencyDataInCheckPoint(Map param, int offset, int limit) {
		return dataQualityDao.queryConsistencyDataInCheckPoint(param, offset, limit);
	}
	
	public long queryConsistencyDataInCheckPointCount(Map param) {
		return dataQualityDao.queryConsistencyDataInCheckPointCount(param);
	}
	
	public List queryAssessmentInCompany(Map param, int offset, int limit) {
		return dataQualityDao.queryAssessmentInCompany(param, offset, limit);
	}
	
	public long queryAssessmentInCompanyCount(Map param) {
		return dataQualityDao.queryAssessmentInCompanyCount(param);
	}
	
	public List queryAssessmentInProduct(Map param, int offset, int limit) {
		return dataQualityDao.queryAssessmentInProduct(param, offset, limit);
	}
	
	public long queryAssessmentInProductCount(Map param) {
		return dataQualityDao.queryAssessmentInProductCount(param);
	}
	
	public List queryAssessmentInCheckPoint(Map param, int offset, int limit) {
		return dataQualityDao.queryAssessmentInCheckPoint(param, offset, limit);
	}
	
	public long queryAssessmentInCheckPointCount(Map param) {
		return dataQualityDao.queryAssessmentInCheckPointCount(param);
	}
	
	public List<HashMap<String, String>> queryDateRecord() {
		List<HashMap<String, String>> resultList = new ArrayList<HashMap<String, String>>();
		List<HashMap<String, String>> recordList = dataQualityDao.queryDateRecord();
		String strStartDate = null;
		String strEndDate = null;
		for (HashMap<String, String> record : recordList) {
			HashMap<String, String> temp = new HashMap<String, String>();
			strStartDate = record.get("startdate").trim();
			strEndDate = record.get("enddate").trim();
			temp.put("value", strStartDate + "," + strEndDate);
			temp.put("text", "起:" + strStartDate + " 止:" + strEndDate);
			resultList.add(temp);
		}
		return resultList;
	}
	
	public List<HashMap<String, String>> queryProduct() {
		return dataQualityDao.queryProduct();
	}
	
	public List<HashMap<String, String>> queryProvince() {
		return dataQualityDao.queryProvince();
	}
	
	public List<HashMap<String, String>> queryCity(Map param) {
		return dataQualityDao.queryCity(param);
	}
	
	public List<HashMap<String, String>> queryCounty(Map param) {
		return dataQualityDao.queryCounty(param);
	}
	
	public List<CompanyTreeNode> createCompanyTree(CompanyTreeNode node) {
		List<HashMap<String, String>> companyList = null;
		Map param = new HashMap();
		param.put("comcode", node.getComcode().trim());
		if ("D".equals(node.getGradelevel().trim())) {
			companyList = dataQualityDao.queryCounty(param);
		} else if ("S".equals(node.getGradelevel().trim())) {
			companyList = dataQualityDao.queryCity(param);
		} else if ("00000000".equals(node.getComcode().trim())) {
			companyList = dataQualityDao.queryProvince();
		}
		List<CompanyTreeNode> resutlTree = makeCompanyTree(companyList);
		return resutlTree;
	}
	
	private List<CompanyTreeNode> makeCompanyTree(List<HashMap<String, String>> companyList) {
		List<CompanyTreeNode> treeList = new ArrayList<CompanyTreeNode>();
		for (HashMap<String, String> company : companyList) {
			CompanyTreeNode treeNode = new CompanyTreeNode();
			treeNode.setComcode(company.get("comcode").trim());
			treeNode.setUppcomcode(company.get("uppcomcode").trim());
			treeNode.setGradelevel(company.get("gradelevel").trim());
			treeNode.setComcname(company.get("comcname").trim());
			treeNode.setFlag(company.get("flag").trim());
			treeNode.setText("["+company.get("comcode").trim()+"]"+company.get("comcname").trim());
			if ("Z".equals(company.get("gradelevel").trim())) {
				treeNode.setLeaf(true);
			} else {
				treeNode.setLeaf(false);
			}
			treeList.add(treeNode);
		}
		return treeList;
	}
	
	public List<HashMap<String, String>> queryCompanyByCode(Map param) {
		return dataQualityDao.queryCompanyByCode(param);
	}
	
	public List<HashMap<String, String>> queryCheckPointList(Map param) {
		return dataQualityDao.queryCheckPointList(param);
	}
	
	public List<HashMap<String, String>> queryInitCheckPoint(Map param) {
		return dataQualityDao.queryInitCheckPoint(param);
	}
	
	public List<HashMap<String, String>> queryPointTableInfo(Map param) {
		return dataQualityDao.queryPointTableInfo(param);
	}
	
	public List<HashMap<String, String>> queryPointHeaderInfo(Map param) {
		return dataQualityDao.queryPointHeaderInfo(param);
	}
	
	public List<HashMap<String, String>> queryWrongListForExport(Map param, int offset, int limit) {
		return dataQualityDao.queryWrongListForExport(param, offset, limit);
	}
	
	public long queryWrongListForExportCount(Map param) {
		return dataQualityDao.queryWrongListForExportCount(param);
	}
}
